<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="com.gzsz.util.DBUtil" %>
<%@ page import="java.text.MessageFormat" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    request.setCharacterEncoding("utf-8");
    String employee_name = request.getParameter("employee_name");
    String occupation = request.getParameter("occupation");
    String schedule_date = request.getParameter("schedule_date");
    String shift_type = request.getParameter("shift_type");
    String start_time = request.getParameter("start_time");
    String end_time = request.getParameter("end_time");

    String id = request.getParameter("id");


    Connection connection = null;
    PreparedStatement preparedStatement = null;
    try {
        DBUtil dbUtil = new DBUtil();
        // 1：加载驱动
//        Class.forName("com.mysql.cj.jdbc.Driver");
//        // 2：创建连接
//        String url = "jdbc:mysql://localhost:3306/medicald?serverTimezone=Asia/Shanghai";
//        String user = "root";
//        String password = "123456";
//        connection = DriverManager.getConnection(url, user, password);
        System.out.println(id);
        if (id == null || id.isEmpty()) {
            System.out.println("add");
            // 3：创建预编译命令对象
            // 假设表名为 patient，根据实际情况修改
            String sql = "INSERT INTO paiban (employee_name, occupation, schedule_date, shift_type, start_time, end_time) " +
                    "VALUES ('" + employee_name + "','" + occupation + "','" + schedule_date + "','" + shift_type + "','" + start_time + "','" + end_time + "');";

            // 4：执行命令对象，返回结果
            int i = dbUtil.executeUpdateBySQL(sql);
            if (i > 0) {
                response.sendRedirect("department-addition.jsp");
            } else {
                out.println("插入失败");
            }
        } else {
            String sql = String.format("UPDATE paiban SET employee_name='%s',occupation='%s',schedule_date ='%s',shift_type = '%s',start_time = '%s',end_time = '%s' WHERE id='%s';",
                    employee_name, occupation, schedule_date, shift_type, start_time, end_time, id);
            System.out.println("update");
            System.out.println(sql);
            // 4：执行命令对象，返回结果
            int i = dbUtil.executeUpdateBySQL(sql);
            if (i > 0) {
                response.sendRedirect("department-addition.jsp");
            } else {
                out.println("修改失败");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        out.println("发生错误：" + e.getMessage());
    } finally {
        // 关闭资源
        try {
            if (preparedStatement != null) preparedStatement.close();
            if (connection != null) connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
%>
